package com.tidc.searabbit.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.tidc.seaservermodel.config.RabbitConstants;
import com.tidc.seaservermodel.entity.*;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaservermodel.exception.TidcRestException;
import com.tidc.seaservermodel.properties.RabbitProperties;
import com.tidc.seaservermodel.util.RestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Map;

/**
 * @ClassNmae MessageConsumer
 * @Description
 * @Author 冯涛滔
 **/
@Component
@Slf4j
public class MessageConsumer {
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private RestUtil restUtil;
    @Resource
    private RabbitProperties rabbitProperties;

    @RabbitListener(queues = RabbitConstants.ADD_EXPERIENCE_QUEUE_NAME)
    @RabbitHandler //标识接收到消息之后处理方法
    public void seaAddExperience(RabbitMessageLog rabbitMessageLog, Channel channel, Message message) throws IOException, TidcRestException {
        UserTask userTask = objectMapper.readValue(rabbitMessageLog.getData(), UserTask.class);
        Fres res = restUtil.post(userTask, rabbitProperties.getUrl(), rabbitProperties.getSeaAddExperienceUri());
    }

    @RabbitListener(queues = RabbitConstants.PUNISHMENT_EXPERIENCE_QUEUE_NAME)
    @RabbitHandler //标识接收到消息之后处理方法
    public void punishmentExperience(RabbitMessageLog rabbitMessageLog, Channel channel, Message message) throws IOException, TidcRestException {
        //body
        UserTask userTask = objectMapper.readValue(rabbitMessageLog.getData(), UserTask.class);
        log.info("惩罚 userTask {}", userTask);
        Fres res = restUtil.post(userTask, rabbitProperties.getUrl(), rabbitProperties.getPunishmentExperienceUri());

    }

    @RabbitListener(queues = RabbitConstants.SEND_EMAIl_QUEUE_NAME)
    @RabbitHandler //标识接收到消息之后处理方法
    public void seaAddEmail(RabbitMessageLog rabbitMessageLog, Channel channel, Message message) throws IOException, TidcRestException {
        String json = rabbitMessageLog.getData();
        Email email = objectMapper.readValue(json, Email.class);
        //body
        Fres res = restUtil.post(email, rabbitProperties.getUrl(), rabbitProperties.getSeaAddEmail());
    }

    @RabbitListener(queues = RabbitConstants.CHECK_TASK_OUT_QUEUE_NAME)
    @RabbitHandler //标识接收到消息之后处理方法
    public void seaTaskCheckTimeout(RabbitMessageLog rabbitMessageLog, Channel channel, Message message) throws IOException, TidcRestException {
        //body
        UserTask userTask = objectMapper.readValue(rabbitMessageLog.getData(), UserTask.class);
        Fres res = restUtil.post(userTask, rabbitProperties.getUrl(), rabbitProperties.getSeaTaskCheckTimeout());
    }

    @RabbitListener(queues = RabbitConstants.CHECK_COMMITMENT_OUT_QUEUE_NAME)
    @RabbitHandler //标识接收到消息之后处理方法
    public void seaCheckCommitmentOut(RabbitMessageLog rabbitMessageLog, Channel channel, Message message) throws IOException, TidcRestException {
        Commitment commitment = objectMapper.readValue(rabbitMessageLog.getData(), Commitment.class);
        //body
        Fres res = restUtil.post(commitment, rabbitProperties.getUrl(), rabbitProperties.getCheckCommitmentOutUri());
    }

    @RabbitListener(queues = RabbitConstants.CHECK_CLEAR_RABBIT_QUEUE_NAME)
    @RabbitHandler //标识接收到消息之后处理方法
    public void clearRabbit(RabbitMessageLog rabbitMessageLog, Channel channel, Message message) throws TidcRestException, IOException {
        //body
//		throw new NullPointerException("dda");
        Fres res = restUtil.post(rabbitMessageLog, rabbitProperties.getUrl(), rabbitProperties.getClearRabbit());
    }


}